<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2017/9/15
 * Time: 下午5:25
 */

namespace app\api\controller;


//use app\api\model\CardCz;
//use app\api\model\Paylog;
//use app\api\model\Wxusergetcard;
//use app\api\model\Wxuserupdate;
use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\OpenTemplate;
use app\common\model\System;
use app\common\model\WxOpen;
use think\Controller;
use Wechat\Wxobjopen;

class Wxtplmesg extends Controller
{

    //余额核销通知

    public function balancesendmesg($arr){
        //wxopenid
        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $map['id'] = $CardMember->where('card_id',$arr['card_id'])->value('mid');
        $wx_open_id = $Merchant->where($map)->value('wx_open_id');
        //初始化微信类
        $options = $this->options($arr['card_id']);
//        file_put_contents('/data2/html/payos/1555.txt',json_encode($options),8);
        $wxObj = new Wxobjopen($options);
        $OpenTemplate= new OpenTemplate();
        $templateid=$OpenTemplate->where(['type'=>'card_ye_tpl','wx_open_id'=>$wx_open_id])->value('template_id');
        switch ($arr['type']) {
            case "1":
                $type = "充值";
                break;
            case "2":
                $type = "消费";
                break;
            default:
                $type = "未知";
                break;
        }
        $add_balance = $arr['add_balance'];
        $balance = $arr['balance'];

        $pay_balance = $arr['pay_balance'];
        $remark1='';
        if($arr['pay_balance']!==0){
            $zs_balance = $add_balance-$pay_balance;
            $remark1="备注：充值:".$pay_balance.'元;'.'赠送:'.$zs_balance."元。\n";

        }
        $template = array(
            'touser' => $arr['openid'],
            'template_id' => trim($templateid),
            'url' => "",
            'topcolor' => "#7B68EE",
            'data' => array(
                'first' => ['value' =>  '余额' . $type . "提醒", 'color' => "#743A3A"],
                'keyword1' => ['value' =>   date("Y-m-d H:i:s", time()), 'color' => '#000'],
                'keyword2' => ['value' =>   '余额' . $type, 'color' => '#FF0000'],
                'keyword3' => ['value' =>   $add_balance.'元', 'color' => '#000'],
                'keyword4' => ['value' =>   $balance.'元','color' => '#000'],
                'remark' => ['value' => $remark1."\n祝您生活愉快", 'color' => '#000'],
            )
        );
        $res = $wxObj->sendTemplateMessage($template);
//        file_put_contents('/data2/html/payos/1555.txt',json_encode($res),8);
        if ($res['errcode'] == 0){
            $rarr = $res;
        }else{
            $rarr = $res;
        }
        $CardUserGet = new CardUserGet();
        $arrp['card_id'] = $arr['card_id'];
        $arrp['code'] = $CardUserGet->where(['openid'=>$arr['openid'],'card_id'=>$arr['card_id']])->value('code');
        $arrp['price'] = $balance;
        $resu = $this->updateprice($arrp);
        return $rarr;
    }

    //余额更新
    public function updateprice($arr){
        //实例化微信
        $options = $this->options($arr['card_id']);
        $WxObj = new Wxobjopen($options);
        $data = [
            'code'=>$arr['code'],
            'card_id'=>$arr['card_id'],
            'custom_field_value2' => $arr['price'].'元',
            'notify_optional'=>[
                'is_notify_bonus'=>false,
                'is_notify_balance'=>false,
                'is_notify_custom_field2'=>false,
            ],
        ];
        $res = $WxObj->updateUserInfo($data);
        return $res;
    }

    //微信API参数  根据card_id获取
    public function options($card_id){
      //  Session::set('openid', null);
        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $System=new System();
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $map['id'] = $CardMember->where('card_id',$card_id)->value('mid');
        $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();

        $mab['id'] = $merchantinfo['wx_open_id'];
//        $config =$System->where(array('id' => 1))->find();
        $users = $WxOpen->where($mab)->find();
        $config=$Agent->where(array('id'=>$merchantinfo['agent']))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }
}